package com.ytjj.qmyx.supplychain.service;

import com.github.pagehelper.PageHelper;
import com.ytjj.qmyx.supplychain.common.api.CommonPage;
import com.ytjj.qmyx.supplychain.common.api.CommonResult;
import com.ytjj.qmyx.supplychain.common.exception.ApiException;
import com.ytjj.qmyx.supplychain.mapper.BankerBankerLabelMapper;
import com.ytjj.qmyx.supplychain.mapper.BankerLabelMapper;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBankerBankerLabel;
import com.ytjj.qmyx.supplychain.mapper.entity.YxBankerLabel;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

@Slf4j
@Service
public class BankerLabelService {
    @Resource
    private BankerLabelMapper bankerLabelMapper;
    @Resource
    private BankerBankerLabelMapper bankerBankerLabelMapper;

    public CommonPage select(String bankerLabelName, Integer pageNum, Integer pageSize) {
        Example example = new Example(YxBankerLabel.class);
        if (!StringUtils.isEmpty(bankerLabelName)) {
            example.createCriteria().andLike("bankerLabelName", bankerLabelName);
        }

        if (null != pageNum && null != pageSize) {
            PageHelper.startPage(pageNum, pageSize);
        }
        List<YxBankerLabel> productLabels = bankerLabelMapper.selectByExample(example);
        productLabels.stream().forEach(item ->{
            Example example1 = new Example(YxBankerBankerLabel.class);
            example1.createCriteria().andEqualTo("bankerLabelId", item.getBankerLabelId());
            long count = bankerBankerLabelMapper.selectCountByExample(example1);

//            BankerBankerLabelExample bankerBankerLabelExample = new BankerBankerLabelExample();
//            bankerBankerLabelExample.createCriteria().andBankerLabelIdEqualTo(item.getBankerLabelId());
//            long count = bankerBankerLabelMapper.countByExample(bankerBankerLabelExample);
            item.setUseNum(Math.toIntExact(count));
//            Admin admin = adminMapper.selectByPrimaryKey(item.getAdminId());
//            item.setAdminName(admin.getCompellation());
        });
        return CommonPage.restPage(productLabels);
    }


    public CommonResult selectNoPage(String bankerLabelName) {
        Example example = new Example(YxBankerLabel.class);
        if (!StringUtils.isEmpty(bankerLabelName)) {
            example.createCriteria().andLike("bankerLabelName", bankerLabelName);
        }
        List<YxBankerLabel> productLabels = bankerLabelMapper.selectByExample(example);
        productLabels.stream().forEach(item ->{
            Example example1 = new Example(YxBankerBankerLabel.class);
            example1.createCriteria().andEqualTo("bankerLabelId", item.getBankerLabelId());
            long count = bankerBankerLabelMapper.selectCountByExample(example1);

//            BankerBankerLabelExample bankerBankerLabelExample = new BankerBankerLabelExample();
//            bankerBankerLabelExample.createCriteria().andBankerLabelIdEqualTo(item.getBankerLabelId());
//            long count = bankerBankerLabelMapper.countByExample(bankerBankerLabelExample);
            item.setUseNum(Math.toIntExact(count));
//            Admin admin = adminMapper.selectByPrimaryKey(item.getAdminId());
//            item.setAdminName(admin.getCompellation());
        });
        return CommonResult.success(productLabels);
    }

    public CommonResult addBankerLabel(List<Integer> bankerIds, List<Integer> bankerLabelIds,Integer adminId) {
        AtomicReference<Integer> count = new AtomicReference<>(0);
        bankerIds.stream().forEach(bankerId ->{
            Example example = new Example(YxBankerBankerLabel.class);
            example.createCriteria().andEqualTo("bankerId", bankerId);
            bankerBankerLabelMapper.deleteByExample(example);

            bankerLabelIds.stream().forEach(bankerLabelId ->{
                YxBankerBankerLabel bankerBankerLabel = new YxBankerBankerLabel();
                bankerBankerLabel.setAdminId(adminId);
                bankerBankerLabel.setBankerId(bankerId);
                bankerBankerLabel.setBankerLabelId(bankerLabelId);
                bankerBankerLabel.setCreateTime(new Date());
                bankerBankerLabelMapper.insertSelective(bankerBankerLabel);
                count.getAndSet(count.get() + 1);
            });
        });
        return CommonResult.success(count.get());
    }

    public CommonResult addAndUpdate(String bankerLabelName, Integer bankerLabelId, Integer adminId, String colour) {
        Example example = new Example(YxBankerLabel.class);
        if (null == bankerLabelId){
            example.createCriteria().andEqualTo("bankerLabelName", bankerLabelName);
            long count = bankerLabelMapper.selectCountByExample(example);
            if (count > 0){
                throw new ApiException("该标签名已存在");
            }
            YxBankerLabel bankerLabel = new YxBankerLabel();
            bankerLabel.setBankerLabelName(bankerLabelName);
            bankerLabel.setCreateTime(new Date());
            bankerLabel.setAdminId(adminId);
            bankerLabel.setColour(colour);
            int i = bankerLabelMapper.insertSelective(bankerLabel);
            if (i > 0){
                return CommonResult.success("添加标签成功");
            }else {
                return CommonResult.failed("添加标签失败");
            }

        }else {
            example.createCriteria().andEqualTo("bankerLabelName", bankerLabelName).andEqualTo("bankerLabelId", bankerLabelId);
            long count = bankerLabelMapper.selectCountByExample(example);
            if (count > 0){
                throw new ApiException("该标签名已存在");
            }
            YxBankerLabel bankerLabel = new YxBankerLabel();
            bankerLabel.setBankerLabelName(bankerLabelName);
            bankerLabel.setAdminId(adminId);
            bankerLabel.setColour(colour);
            bankerLabel.setBankerLabelId(bankerLabelId);
            int i = bankerLabelMapper.updateByPrimaryKeySelective(bankerLabel);
            if (i > 0){
                return CommonResult.success("修改标签成功");
            }else {
                return CommonResult.failed("修改标签失败");
            }
        }
    }

    public CommonResult delete(Integer bankerLabelId) {
        Example example = new Example(YxBankerBankerLabel.class);
        example.createCriteria().andEqualTo("bankerLabelId", bankerLabelId);
        long count = bankerBankerLabelMapper.selectCountByExample(example);
        if (count > 0){
            throw new ApiException("该标签正在使用,不能删除");
        }
        int i = bankerLabelMapper.deleteByPrimaryKey(bankerLabelId);
        if (i > 0){
            return CommonResult.success("删除成功");
        }else {
            return CommonResult.failed("删除失败");
        }
    }
}
